//
//  CourseDetailsViewController.m
//  ZHIDOU
//
//  Created by 向礼 on 2018/12/21.
//  Copyright © 2018 向礼. All rights reserved.
//

#import "CourseDetailsViewController.h"
#import "SPPageMenu.h"
#import "OneOfCourseViewController.h"
#import "OrderPayViewController.h"

@interface CourseDetailsViewController ()<SPPageMenuDelegate,UIScrollViewDelegate>
//滚动控制器
@property (nonatomic, strong) SPPageMenu *pageMenu;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *myChildViewControllers;

///购买按钮
@property (nonatomic, strong)UIButton *BuyButton;

@end

@implementation CourseDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程详情";
    [self SubViews];
   // self.view.backgroundColor = [UIColor c];

    // Do any additional setup after loading the view.
}

#pragma mark - UI
-(void)SubViews{
    // 添加一个全屏的scrollView
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageMenu];
    [self.view addSubview:self.BuyButton];
    for (int i =0; i<2; i++) {
        OneOfCourseViewController *one = [[OneOfCourseViewController alloc] init];
        one.IsCourse = YES;
        // OneOfCourseViewController *one = [[OneOfCourseViewController alloc]init];
        [self addChildViewController:one];
        [self.myChildViewControllers addObject:one];
    }
    [self.scrollView addSubview:self.childViewControllers[0].view];
    _pageMenu.bridgeScrollView = self.scrollView;
    
}

#pragma mark - PageMenu
- (SPPageMenu *)pageMenu {
    
    if (!_pageMenu) {
        _pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 1, ZD_screenWidth, 45) trackerStyle:SPPageMenuTrackerStyleLine];
       // _pageMenu.trackerWidth = 30;
        [_pageMenu setTrackerHeight:2 cornerRadius:0];
        _pageMenu.dividingLine.hidden = YES;
        [_pageMenu setItems:@[@"第一章 函数",@"第二章 有理数"] selectedItemIndex:0];
        _pageMenu.delegate = self;
        _pageMenu.itemTitleFont = [UIFont systemFontOfSize:15];
        _pageMenu.selectedItemTitleColor = UIColorFromRGB(0x222222);
        _pageMenu.unSelectedItemTitleColor = UIColorFromRGB(0x999999);
        _pageMenu.tracker.backgroundColor = UIColorFromRGB(0x222222);
        _pageMenu.permutationWay = SPPageMenuPermutationWayScrollAdaptContent;
        //  _pageMenu.trackerStyle = SPPageMenuTrackerStyleLineAttachment;
        //_pageMenu.bridgeScrollView = self.scrollView;
        
    }
    return _pageMenu;
}


- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, 44, ZD_screenWidth, ZD_screenHeight-self.bottomSafeAreaInsets - 94);
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(ZD_screenWidth*2, 0);
       // _scrollView.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    }
    return _scrollView;
}

- (UIButton *)BuyButton{
    if (_BuyButton == nil) {
        _BuyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _BuyButton.backgroundColor = UIColorFromRGB(0xFFD62C);
        _BuyButton.frame = CGRectMake(0, ZD_screenHeight-50-self.bottomSafeAreaInsets, ZD_screenWidth, 50);
        [_BuyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_BuyButton setTitle:@"￥599 购买该课程" forState:UIControlStateNormal];
        [_BuyButton addTarget:self action:@selector(BuyAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _BuyButton;
}

#pragma mark - f购买
- (void)BuyAction{
    OrderPayViewController *pay = [[OrderPayViewController alloc]init];
    [self.navigationController pushViewController:pay animated:YES];
    
}

#pragma mark - SPPageMenu的代理方法

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index {
    NSLog(@"%zd",index);
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    NSLog(@"%zd------->%zd",fromIndex,toIndex);
    
    // 如果该代理方法是由拖拽self.scrollView而触发，说明self.scrollView已经在用户手指的拖拽下而发生偏移，此时不需要再用代码去设置偏移量，否则在跟踪模式为SPPageMenuTrackerFollowingModeHalf的情况下，滑到屏幕一半时会有闪跳现象。闪跳是因为外界设置的scrollView偏移和用户拖拽产生冲突
    if (!self.scrollView.isDragging) { // 判断用户是否在拖拽scrollView
        // 如果fromIndex与toIndex之差大于等于2,说明跨界面移动了,此时不动画.
        if (labs(toIndex - fromIndex) >= 2) {
            [self.scrollView setContentOffset:CGPointMake(ZD_screenWidth * toIndex, 0) animated:NO];
        } else {
            [self.scrollView setContentOffset:CGPointMake(ZD_screenWidth * toIndex, 0) animated:YES];
        }
    }
    
    if (self.myChildViewControllers.count <= toIndex) {return;}
    
    UIViewController *targetViewController = self.myChildViewControllers[toIndex];
    // 如果已经加载过，就不再加载
    if ([targetViewController isViewLoaded]) return;
    
    targetViewController.view.frame = CGRectMake(ZD_screenWidth * toIndex, 0, ZD_screenWidth, ZD_screenHeight-44);
    [_scrollView addSubview:targetViewController.view];
    
}


- (NSMutableArray *)myChildViewControllers {
    if (!_myChildViewControllers) {
        _myChildViewControllers = [NSMutableArray array];
        
    }
    return _myChildViewControllers;
}

@end
